namespace BM.Tools.WebControls.MForm.Controls.Tabs
{
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using BM.Tools.WebControls.MForm.Controls.Helper;

    /// <summary>
    /// The tab that can be created inside the TabContainer
    /// </summary>
    [DesignTimeVisible(false)]
    public class Tab : WebControl, ITextControl
    {
        private const string TabCssClass = "Tb";

        private const string SelectedCssClass = "Selected";

        private const string InvisibleCssClass = "Invisible";

        /// <summary>
        /// is selected field
        /// </summary>
        private bool selected;

        /// <summary>
        /// target control field
        /// </summary>
        private WebControl targetControl;

        /// <summary>
        /// Gets ClientID.
        /// </summary>
        /// <value>
        /// The client id.
        /// </value>
        public override string ClientID
        {
            get
            {
                if (DesignMode)
                {
                    return null;
                }

                return string.Concat(TargetControl.ClientID, "__", TabCssClass);
            }
        }

        /// <summary>
        /// Gets CssClass.
        /// </summary>
        /// <value>
        /// The css class.
        /// </value>
        public override string CssClass
        {
            get
            {
                string cssClass = TabCssClass;
                if (Selected)
                {
                    cssClass += " " + SelectedCssClass;
                }

                return cssClass;
            }
        }

        /// <summary>
        /// Gets OnClickFunction.
        /// </summary>
        /// <value>
        /// The on click function.
        /// </value>
        public virtual string OnClickFunction
        {
            get
            {
                return "BM.Tab.choose(this);";
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether Selected.
        /// </summary>
        /// <value>
        /// The selected.
        /// </value>
        public bool Selected
        {
            get
            {
                return selected;
            }

            set
            {
                selected = value;
                if (value)
                {
                    OnTabSelected();
                }
                else
                {
                    OnTabDeselected();
                }
            }
        }

        /// <summary>
        /// Gets or sets TargetControl.
        /// </summary>
        /// <value>
        /// The target control.
        /// </value>
        public WebControl TargetControl
        {
            get
            {
                if (DesignMode)
                {
                    return null;
                }

                if (targetControl == null)
                {
                    targetControl = Parent.NamingContainer.FindControl(TargetControlId) as WebControl;
                }

                return targetControl;
            }

            set
            {
                targetControl = value;
            }
        }

        /// <summary>
        /// Gets or sets TargetControlId.
        /// </summary>
        /// <value>
        /// The target control id.
        /// </value>
        [IDReferenceProperty(typeof(WebControl))]
        public string TargetControlId
        {
            get;

            set;
        }

        /// <summary>
        /// Gets or sets Text.
        /// </summary>
        /// <value>
        /// The control text.
        /// </value>
        public virtual string Text
        {
            get;

            set;
        }

        /// <summary>
        /// the div tag key
        /// </summary>
        /// <value>
        /// The div tag key.
        /// </value>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// adds attributes needed by the tab
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
            if (!string.IsNullOrEmpty(OnClickFunction))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, OnClickFunction);
            }
        }

        /// <summary>
        /// runs actions needed when tab is deselected
        /// </summary>
        protected virtual void OnTabDeselected()
        {
            if (DesignMode)
            {
                return;
            }

            var classes = new CssClasses(TargetControl);
            classes.Add(InvisibleCssClass);
        }

        /// <summary>
        /// runs actions needed when tab is selected
        /// </summary>
        protected virtual void OnTabSelected()
        {
            if (DesignMode)
            {
                return;
            }

            var classes = new CssClasses(TargetControl);
            classes.Remove(InvisibleCssClass);
        }

        /// <summary>
        /// Render the contents of the tab - two inner divs
        /// </summary>
        /// <param name="writer">
        /// A <see cref="T:System.Web.UI.HtmlTextWriter"></see> that represents the output stream to render HTML content on the client. 
        /// </param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write(@"<div class=""TbL""><div class=""TbR"">");
            writer.Write(Text);
            base.RenderContents(writer);
            writer.Write(@"</div></div>");
        }
    }
}